package com.itheima.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.common.pojo.Employee;
import com.itheima.common.result.CommonResult;
import com.itheima.common.result.PageInfo;
import com.itheima.common.result.code;
import com.itheima.common.vo.EmployeeVO;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author liguoping
 */
@RestController
@RequestMapping("employee")
@Slf4j
public class EmployeeController {

    //Autowired:按照类的类型去注入
    //@Resource:先按照类型去注入，然后在按照名称去注入；
    @Autowired
    public EmployeeService employeeService;

    @PostMapping("login")
    public CommonResult login(@RequestBody Employee employee) {
//        判断登录是否成功
        if (ObjectUtil.isEmpty(employee)) {
            return CommonResult.error("对象为空" + code.FORBIDDEN);
        }
        if (StrUtil.isBlank(employee.getUsername())) {
            return CommonResult.error("名字不为空" + code.FORBIDDEN);
        }
        if (StrUtil.isBlank(employee.getPassword())) {
            return CommonResult.error("密码不为空" + code.FORBIDDEN);
        }
        EmployeeVO employeeVO = employeeService.login(employee.getUsername(), employee.getPassword());
        if (employeeVO == null) {
            return CommonResult.error("用户名密码错误" + code.NOT_FOUND);
        }
        return CommonResult.success(employeeVO);
    }

//    @GetMapping
//    public CommonResult getEmployee() {
//        return CommonResult.success("访问成功");
//    }

    @PostMapping("logout")
    public CommonResult logout(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        employeeService.clearToken(token);
        return CommonResult.success("退出登录成功");
    }

    @PostMapping
    public CommonResult save(@RequestBody Employee employee) {
        boolean flag = employeeService.save(employee);
        if (flag) {
            return CommonResult.success("添加成功");
        }
        return CommonResult.error("添加失败" + code.CREATED);
    }

    @GetMapping("/{id}")
    public CommonResult getById(@PathVariable("id") String id) {
        EmployeeVO vo = employeeService.getById(id);
        if (vo == null) {
            return CommonResult.error("没找到该用户" + code.NOT_FOUND);
        }
        return CommonResult.success(vo);
    }

    @GetMapping("page")
    public CommonResult page(String name,
                             @RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer pagesize) {
        PageInfo<Employee> page1 = employeeService.page(name, page, pagesize);
        return CommonResult.success(page1);
    }

    @PutMapping
    public CommonResult updateEmployee(@RequestBody Employee employee) {
        //todo:无法解决时间戳转LocalDateTime
        boolean flag = employeeService.updateEmployee(employee);
        if (flag) {
            return CommonResult.success("修改成功");
        }
        return CommonResult.error("修改失败" + code.NOT_FOUND);
    }
}
